import { ref, computed } from "vue";
import { defineStore } from "pinia";
import { useI18n } from "vue-i18n";
import { usePrimeVue } from "primevue/config";
import { getLocaleMessages } from "@/config/primevue.config";
import appConfig from "@/config/app.config";
export const useLangStore = defineStore("lang", () => {
  const i18n = useI18n();
  const primeVue = usePrimeVue();

  const langOptions = appConfig.langOptions;
  const lang = ref(appConfig.defaultLang());
  // const selectedLocale = ref(langOptions[0]);
  const setLang = (newLang: string) => {
    if (lang.value === newLang) return;
    lang.value = newLang;
    i18n.locale.value = newLang;
    primeVue.config.locale = getLocaleMessages(newLang);
    localStorage.setItem(appConfig.LANG_SESSION_KEY, newLang);
  };
  const selectedLocale = computed(() => {
    return (
      langOptions.find((option) => option.lang === lang.value) || langOptions[0]
    );
  });

  return {
    langOptions,
    lang,
    selectedLocale,
    setLang,
  };
});
